package com.ftg.learn.sys.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.comm.entity.sys.Resources;
import com.ftg.learn.sys.user.mapper.ResourcesMapper;
import com.ftg.learn.sys.user.service.IResourcesService;
import com.ftg.learn.util.MapperUtil;
import com.ftg.learn.util.ResBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 资源表（权限表|菜单表） 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2022-10-30
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IResourcesService {

    @Resource
    MapperUtil mapperUtil;

    @Resource
    ResourcesMapper resourcesMapper;

    @Override
    public List<Resources> showResourceByUid(Integer uid) {
        return this.baseMapper.showResourceByUid(uid);
    }


    @Override
    public ResBean showResourceByRoleid(Integer rid) {
        String sql = "select a.* from sys_resources a left join sys_role_resources b" +
                " on a.id = b.res_id" +
                " left join sys_role c on b.r_id = c.id where c.id = 1";
        List<Map<String,Object>> list = mapperUtil.utilSelect(sql);
        if(Objects.isNull(list) || list.isEmpty()){
            return ResBean.noData();
        }
        return ResBean.success(list);
    }

    @Override
    public ResBean showAll() {
        List<Resources> list = this.baseMapper.selectList(null);
        if(Objects.isNull(list)){
            return ResBean.noData();
        }
        return ResBean.success(list);
    }

    @Override
    public ResBean udpateResName(Integer resid, String resName) {
        int num = resourcesMapper.udpateResName(resid,resName);
        if(num == 0){
            return  ResBean.failure();
        }
        return ResBean.success();
    }

}
